package com.cm.figure.controller.user.login;

import com.cm.figure.model.User;
import com.cm.figure.service.userService.LoginService;
import com.cm.figure.utility.BuildData;
import com.cm.figure.utility.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * ClassName:Login
 * Package:com.cm.figure.controller.user.login
 * Description:
 *
 * @Date:2021/8/14 22:08
 * @com.chuangmei
 */
@Controller
@RequestMapping("/api/private/figure/")
public class Login {

    // service
    @Autowired
    private LoginService loginService;

    @PostMapping("login")
    @ResponseBody
    public Object login(@RequestBody User user) {

        // 默认状态
        String message = "登录成功!";
        Integer status = 200;

        // 登录并返回user
        User responseUser = loginService.login(user.getUsername(),user.getPassword());

        // 修改登录状态
        if (null == responseUser) {
            message = "登录失败!";
            status = 401;
        }else {
            String token = Token.getToken(responseUser.getUsername(),responseUser.getId().intValue());
            responseUser.setToken(token);
        }

        return BuildData.buildData(responseUser,message,status);
    }

}
